// TransparentStatic2.cpp : implementation file
//

#include "stdafx.h"
#include "CStaticEx.h"


// CStaticEx

IMPLEMENT_DYNAMIC(CStaticEx, CStatic)
CStaticEx::CStaticEx()
{
     m_TextClr = RGB(255,0,0);
	 m_Cursor = NULL;
     ::GetObject((HFONT)GetStockObject(DEFAULT_GUI_FONT),sizeof(m_LogFont),&m_LogFont);
     m_Font.CreateFontIndirect(&m_LogFont);
}

CStaticEx::~CStaticEx()
{
     m_Font.DeleteObject();
}


BEGIN_MESSAGE_MAP(CStaticEx, CStatic)
     ON_MESSAGE(WM_SETTEXT,OnSetText)
     ON_WM_CTLCOLOR_REFLECT()
     ON_WM_ERASEBKGND()
	 ON_WM_SETCURSOR()
	 //ON_WM_MOUSEMOVE()
END_MESSAGE_MAP()



// CStaticEx message handlers

LRESULT CStaticEx::OnSetText(WPARAM wParam,LPARAM lParam)
{
     LRESULT Result = Default();
     Invalidate();
     UpdateWindow();
     return Result;
}

HBRUSH CStaticEx::CtlColor(CDC* pDC, UINT /*nCtlColor*/)
{
     pDC->SelectObject(&m_Font);
     pDC->SetTextColor(m_TextClr);
     pDC->SetBkMode(TRANSPARENT);
     return (HBRUSH)GetStockObject(NULL_BRUSH);
}

BOOL CStaticEx::OnEraseBkgnd(CDC* pDC)
{
   if (m_BgBmp.GetSafeHandle() == NULL)
   {
      CRect Rect;
      GetWindowRect(&Rect);
      CWnd *pParent = GetParent();
      ASSERT(pParent);
      pParent->ScreenToClient(&Rect);  //convert our corrdinates to our parents
      
      //copy what's on the parents at this point
      CDC *pDC = pParent->GetDC();
      CDC MemDC;
      MemDC.CreateCompatibleDC(pDC);
      m_BgBmp.CreateCompatibleBitmap(pDC,Rect.Width(),Rect.Height());
      CBitmap *pOldBmp = MemDC.SelectObject(&m_BgBmp);
      MemDC.BitBlt(0,0,Rect.Width(),Rect.Height(),pDC,Rect.left,Rect.top,SRCCOPY);
      MemDC.SelectObject(pOldBmp);
      pParent->ReleaseDC(pDC);
   }
   else //copy what we copied off the parent the first time back onto the parent
   {
      CRect Rect;
      GetClientRect(Rect);
      CDC MemDC;
      MemDC.CreateCompatibleDC(pDC);
      CBitmap *pOldBmp = MemDC.SelectObject(&m_BgBmp);
      pDC->BitBlt(0,0,Rect.Width(),Rect.Height(),&MemDC,0,0,SRCCOPY);
      MemDC.SelectObject(pOldBmp);
   }

   return TRUE;
}

void CStaticEx::SetTextColor(COLORREF Color)
{
     m_TextClr = Color;
}

void CStaticEx::SetCtrlCursor(HCURSOR Cursor)
{
     ASSERT(Cursor);
     m_Cursor = Cursor;
}

void CStaticEx::SetFontBold(BOOL Bold)
{	
     m_LogFont.lfWeight = Bold ? FW_BOLD : FW_NORMAL;
     ReconstructFont();
     RedrawWindow();
}

void CStaticEx::SetFontUnderline(BOOL Set)
{	
     m_LogFont.lfUnderline = Set;
     ReconstructFont();
     RedrawWindow();
}

void CStaticEx::SetFontItalic(BOOL Set)
{
     m_LogFont.lfItalic = Set;
     ReconstructFont();
     RedrawWindow();	
}

void CStaticEx::ReconstructFont()
{
	 m_Font.DeleteObject();
	 BOOL IsCreated = m_Font.CreateFontIndirect(&m_LogFont);
	 ASSERT(IsCreated);
}

BOOL CStaticEx::OnSetCursor(CWnd* pWnd, UINT nHitTest, UINT message) 
{
	if( m_Cursor )
	{
		::SetCursor(m_Cursor);
		return TRUE;
	}

	return CStatic::OnSetCursor(pWnd, nHitTest, message);
}

//void CStaticEx::OnMouseMove(UINT nFlags, CPoint Point) 
//{
//	if( m_Cursor )
//	{
//		::SetCursor(m_Cursor);
//	}
//
//     CStatic::OnMouseMove(nFlags, Point);
//}